#磁盘
$disk = @ARGV[0];
if($disk !~ /^\/dev\/[a-z]?[\/a-z]*[a-z]$/){
	die "$disk fromater error";
}
print("disk path $disk");
#用户名
$username = @ARGV[1];
#密码
$password = @ARGV[2];
print("user name : $username");
print("user password : $password");
#磁盘分区
system("fdisk $disk<<EOF\nn\np\n \n \n+128M\nw\nEOF");
system("fdisk $disk<<EOF\nn\np\n \n \n \nw\nEOF");
system("`fdisk -l");

#更新安装源
system("sed -i '11iServer = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch' /etc/pacman.d/mirrorlist");
$archlinuxcn="[archlinuxcn]\nSigLevel = Optional TrustAll\nServer = http://repo.archlinuxcn.org/\\\$arch";
system("cat>> /etc/pacman.conf<<EOF\n$archlinuxcn\nEOF");
system("pacman -Syy");

#格式化磁盘
system("mkfs.vfat $disk".1);
system("mkfs.ext4 $disk"."2<<EOF\ny\nEOF");
#挂在分区
system("mount $disk"."2 /mnt");
system("mkdir -p /mnt/boot");
system("mount $disk"."1 /mnt/boot");

#安装根系统
$pacstrap="pacstrap /mnt base base-devel linux linux-firmware vim dnsutils inetutils iproute2 dhcpcd wget curl bash zsh git grub os-prober efibootmgr<<EOF\ny\nEOF";
system("$pacstrap");

#生成fstab文件
system("genfstab -U /mnt >> /mnt/etc/fstab");
system("echo 'workspace' >> /mnt/etc/hostname");
system("echo 'LANG=en_US.UTF-8' >> /mnt/etc/locale.conf");
system("sed -i 's/#en_US.UTF-8/en_US.UTF-8/g' /mnt/etc/locale.gen");
system("sed -i 's/#zh_CN.UTF-8/zh_CN.UTF-8/g' /mnt/etc/locale.gen");
system("sed -i 's/#zh_CN.GBK/zh_CN.GBK/g' /mnt/etc/locale.gen");
#安装其它软件
@archchroot = (
    "locale-gen",
	"dd if=/dev/zero of=/swapfile bs=1M count=2048",
	"mkswap /swapfile",
	"chmod 600 /swapfile",
	"swapon /swapfile",
	"passwd<<EOF\n$password\n$password\nEOF",
	"useradd -d /home/$username -m -r $username",
	"passwd $username<<EOF\n$password\n$password\nEOF",
	"sed -i \"80i$username  ALL=(ALL)	ALL\"   /etc/sudoers",
	"systemctl enable dhcpcd",
	"pacman -S xorg xorg-xinit<<EOF\n \ny\nEOF",
	"mkdir -p /home/$username/bin",
	"chown -R $username:$username /home/$username",
	"grub-install --target=i386-pc $disk",
	"grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=grub",
	"grub-mkconfig -o /boot/grub/grub.cfg"
);
foreach $cmd(@archchroot){
	print("$cmd\n");
	system("arch-chroot /mnt $cmd");
}
system("echo '/swapfile swap swap defaults 0 0'>>/mnt/etc/fstab");
system("cp /etc/pacman.conf /mnt/etc/pacman.conf");
system("cp /etc/pacman.d/mirrorlist /mnt/etc/pacman.d/mirrorlsit");
